package data;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Pedido;
import domain.Receptor;
import domain.Turnos;

public class JDBCPedidoDAO implements IPedidoDAO{
	private Connection conn;
	private ILineaPedidoDAO lpdao;
	private IReceptorDAO rdao;
	
	public JDBCPedidoDAO() {
        conn = ConnectionManager.getInstance().checkOut();
        lpdao = new JDBCLineaPedidoDAO();
        rdao = new JDBCReceptorDAO();
    }
        
	
	public void delete(String numPedido) {
		String sql = "DELETE FROM Pedidos WHERE (orderid = ?) ";
		PreparedStatement stmt = null;
		
		String pedidoOID = selectOIDDePedido(numPedido);
		
		 if (pedidoOID!=null) {
			 int receptorOID = selectOIDDeReceptor(pedidoOID);
			 
			 rdao.delete(conn, receptorOID);
		     lpdao.delete(conn, pedidoOID);
		 try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, numPedido);
	            stmt.executeUpdate();
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
	        }
	    }
	
	
	private int selectOIDDeReceptor(String pedidoOID) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        int receptorOID = 0;
        String sql = "SELECT * FROM Pedidos WHERE (OID = ? ) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, pedidoOID);

            result = stmt.executeQuery();
            result.next();
            receptorOID = result.getInt("receptorOID");
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }

                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return receptorOID;
	}


	private String selectOIDDePedido(String numPedido) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oid = null;
        String sql = "SELECT * FROM Pedidos WHERE (orderid = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, numPedido);
            result = stmt.executeQuery();

            if (result.next()) {
                oid = result.getString("OID");
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oid;
	}


	
    public void insertPedido(Pedido p) {
	}

	
	public List<Pedido> selectAllPedidos() {
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        List listaPedido = new LinkedList();

	        
	        String sql = "SELECT * FROM Pedidos";

	        try {
	            stmt = conn.prepareStatement(sql);
	            result = stmt.executeQuery();

	            while (result.next()) {
	                Pedido p = new Pedido();
	                String pedidoOid = result.getString("pedidoOID");
	                p.setPedidoID(result.getString("pedidoID"));
	                p.setFechaPedido(new Date(result.getLong("fechaPedido")));
	                
	                //get Receptor
	                Receptor recept=rdao.select(conn,result.getInt("ReceptorOID"));
	                p.setReceptor(recept);

	               //get Turno

	                //p.setTurno(new Turnos());
	                p.setLineaPedido(lpdao.select(conn, pedidoOid));
	                //get Address
	                p.setFechaEntrega(new Date(result.getLong("fechaEntrega")));

	                p.setValidado(result.getBoolean("validado"));
	            
	                listaPedido.add(p);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }

	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return listaPedido;
	    }

}
